﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace WindowsGame1.Model
{
    class Enemy
    {
        Vector2 _poisiton = new Vector2(0f, 0f);
        Vector2 _speed = new Vector2(-2f, 0f);
        public Vector2 _sizes = new Vector2(0.9f, 0.9f);
        Vector2 _velocity = Vector2.Zero;
        
        public void SetPosition(float a_x, float a_y)
        {
            _poisiton.X = a_x;
            _poisiton.Y = a_y;
        }

        public Vector2 GetPosition()
        {
            return _poisiton;
        }

        public Vector2 GetSpeed()
        {
            return _speed;
        }

        public void SetSpeed(float a_x, float a_y)
        {
            _speed.X = a_x;
            _speed.Y = a_y;
        }

        internal void Update(float a_elapsedTime, Level a_level)
        {
            Vector2 gravityAcceleration = new Vector2(0.0f, 14.00f);

            _poisiton = _poisiton + _speed * a_elapsedTime + gravityAcceleration * a_elapsedTime * a_elapsedTime;
            
            if (NoGroundToWalkOn(a_level) == true)
            {
                TurnAround();
            }

            _speed = _speed + a_elapsedTime * gravityAcceleration;
        }

        private bool NoGroundToWalkOn(Level a_level)
        {
            if (a_level._tiles[(int)(_poisiton.X + 1), (int)_poisiton.Y - 1] == Level.Tile.T_EMPTY)
            {
                return true;
            }
            if (a_level._tiles[(int)(_poisiton.X +1), (int)_poisiton.Y] == Level.Tile.T_EMPTY)
            {
                return true;
            }

            return false;
        }

        private void TurnAround()
        {
            if (_speed.X == 2)
            {
                SetSpeed(-2f, 0);
            }
            else
            {
                SetSpeed(2f, 0);
            }
        }


    }
}
